<template>
    <div>
        <city-header></city-header>
        <city-search :cities="data.cities" :alphabet="alphabet"></city-search>
        <city-list :hotCitiesList="data.hotCities" :cities="data.cities" :letter="data.letter"></city-list>
        <city-alphabet :alphabet="alphabet" @change="changeLetter"></city-alphabet>
    </div>
</template>
<script>
import CityHeader from './components/Header'
import CitySearch from './components/Search'
import CityList from './components/List'
import CityAlphabet from './components/Alphabet'
import axios from 'axios'
import { reactive, computed, onMounted } from 'vue'
import { useStore } from 'vuex'
export default {
  name: 'City',
  components: {
    CityHeader,
    CitySearch,
    CityList,
    CityAlphabet
  },
  setup(){
    let data = reactive({
      hotCities: [],
      cities: {},
      letter: ''
    })
    let alphabet = computed(()=>{
      return Object.keys(data.cities)
    })
    let store = useStore()
    let city = store.state.city
    function changeLetter(le){
      data.letter = le
    }
    async function getCityData(){
      let res = await axios.get('/api/city.json')
      let result = res.data
      if (result.ret && result.data) {
        result = result.data
        data.hotCities = result.hotCities
        data.cities = result.cities
      }
    }
    onMounted(()=>{
      getCityData()
    }) 
    return { data, alphabet, changeLetter }
  }
}
</script>
